package cn.szzsi.common.util;

import cn.szzsi.AConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class AddressUtil {

    public JSONObject getLonAndLatByAddress(String address){
        JSONObject jsonResult=new JSONObject();
        String url="https://api.map.baidu.com/geocoder/v2/?";
        try {
            address= URLEncoder.encode(address,"utf-8");
            String para="address="+address+"&output=json&ak="+ AConfig.p.get("BAIDU_KEY")+"&ret_coordtype=gcj02ll";
            String result= HttpKit.get(url+para);
            JSONObject jsonObject= JSON.parseObject(result);
            String longitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lng").toString();
            String latitude=jsonObject.getJSONObject("result").getJSONObject("location").get("lat").toString();
            jsonResult.put("longitude",longitude);
            jsonResult.put("latitude",latitude);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return jsonResult;
    }
}
